// ============================================================================
// 基础数据模型
// ============================================================================

/**
 * 用户信息接口
 */
export interface User {
  id: number
  username: string
  nickname: string
  avatar: string
  status: 'online' | 'offline' | 'away'
  lastActiveTime: Date
}

/**
 * 好友信息接口
 */
export interface Friend extends User {
  isFriend: boolean
  friendshipStatus: 'pending' | 'accepted' | 'blocked'
  addedAt?: Date
  remark?: string
  groupId?: number
  groupName?: string
}

/**
 * 好友请求状态枚举
 */
export type FriendRequestStatus = 'PENDING' | 'ACCEPTED' | 'REJECTED' | 'EXPIRED' | 'CANCELLED'

/**
 * 好友请求接口
 */
export interface FriendRequest {
  id: number
  fromUserId: number
  toUserId: number
  message?: string
  status: FriendRequestStatus
  rejectReason?: string
  createdAt: Date
  updatedAt: Date
  processedAt?: Date
  expiredAt?: Date
  fromUser?: User
  toUser?: User
}

/**
 * 好友分组接口
 */
export interface FriendGroup {
  id: number
  userId: number
  name: string
  description?: string
  sortOrder: number
  isDefault: boolean
  isSystem: boolean
  createdAt: Date
  updatedAt: Date
  friendCount?: number
}

/**
 * 用户隐私设置接口
 */
export interface UserPrivacySetting {
  id: number
  userId: number
  friendRequestPermission: 'EVERYONE' | 'FRIENDS_OF_FRIENDS' | 'CONTACTS_ONLY' | 'NOBODY'
  requireVerification: boolean
  allowSearchByPhone: boolean
  allowSearchByEmail: boolean
  allowSearchByUsername: boolean
  showOnlineStatus: boolean
  allowGroupInvite: boolean
  createdAt: Date
  updatedAt: Date
}

/**
 * 用户状态枚举
 */
export type UserStatus = 'online' | 'offline' | 'away'

/**
 * 消息类型枚举
 */
export type MessageType = 'TEXT' | 'IMAGE' | 'FILE' | 'VOICE' | 'SYSTEM'

/**
 * 消息状态枚举
 */
export type MessageStatus = 'SENDING' | 'SENT' | 'DELIVERED' | 'READ' | 'FAILED'

/**
 * 消息内容接口
 */
export interface MessageContent {
  text?: string
  imageUrl?: string
  fileUrl?: string
  fileName?: string
  fileSize?: number
  duration?: number // 语音消息时长（秒）
  thumbnailUrl?: string // 图片缩略图
}

/**
 * 消息接口
 */
export interface Message {
  messageId: string
  fromUserId: number
  toUserId?: number
  groupId?: number
  conversationId: string
  messageType: MessageType
  content: MessageContent
  status: MessageStatus
  createdAt: Date
  sequence: number
  isRead: boolean
  senderNickname: string
  senderAvatarUrl: string
  replyToMessageId?: string
}

/**
 * 会话类型枚举
 */
export type ConversationType = 'PRIVATE' | 'GROUP'

/**
 * 会话接口
 */
export interface Conversation {
  id: string
  type: ConversationType
  participants: number[]
  lastMessage?: Message
  unreadCount: number
  title: string
  avatar: string
  updatedAt: Date
  createdAt: Date
  isActive: boolean
  isMuted: boolean
}

// ============================================================================
// API 请求和响应类型
// ============================================================================

/**
 * 通用API响应接口
 */
export interface ApiResponse<T = any> {
  success: boolean
  data?: T
  message?: string
  code?: number
  timestamp?: Date
}

/**
 * 分页响应接口
 */
export interface PaginatedResponse<T> {
  items: T[]
  total: number
  page: number
  pageSize: number
  hasNext: boolean
  hasPrev: boolean
}

/**
 * 登录请求接口
 */
export interface LoginRequest {
  username: string
  password: string
  rememberMe?: boolean
}

/**
 * 登录响应接口
 */
export interface LoginResponse {
  user: User
  token: string
  refreshToken: string
  expiresIn: number
}

/**
 * 用户信息响应接口
 */
export interface UserInfo extends User {
  email?: string
  phone?: string
  createdAt: Date
  updatedAt: Date
}

/**
 * 发送消息请求接口
 */
export interface SendMessageRequest {
  conversationId: string
  messageType: MessageType
  content: MessageContent
  replyToMessageId?: string
  clientMessageId?: string // 客户端生成的临时ID
}

/**
 * 消息响应接口
 */
export interface MessageResponse extends Message {
  // 继承Message的所有属性
}

/**
 * 获取消息历史请求接口
 */
export interface GetMessagesRequest {
  conversationId: string
  page?: number
  pageSize?: number
  beforeMessageId?: string
  afterMessageId?: string
}

/**
 * 标记消息已读请求接口
 */
export interface MarkMessageReadRequest {
  messageId: string
  conversationId: string
}

/**
 * 获取好友列表响应接口
 */
export interface FriendListResponse {
  friends: Friend[]
  onlineCount: number
  totalCount: number
}

/**
 * 搜索好友请求接口
 */
export interface SearchFriendsRequest {
  keyword: string
  page?: number
  pageSize?: number
}

/**
 * 会话列表响应接口
 */
export interface ConversationListResponse {
  conversations: Conversation[]
  totalUnreadCount: number
}

// ============================================================================
// 好友请求相关API类型
// ============================================================================

/**
 * 发送好友请求接口
 */
export interface SendFriendRequestRequest {
  fromUserId: number
  toUserId: number
  message?: string
}

/**
 * 处理好友请求接口
 */
export interface ProcessFriendRequestRequest {
  userId: number
}

/**
 * 拒绝好友请求接口
 */
export interface RejectFriendRequestRequest {
  userId: number
  reason?: string
}

/**
 * 好友请求分页响应接口
 */
export interface FriendRequestPageResponse {
  content: FriendRequest[]
  totalElements: number
  totalPages: number
  currentPage: number
  pageSize: number
}

/**
 * 待处理请求数量响应接口
 */
export interface PendingRequestCountResponse {
  count: number
}

/**
 * 是否可以发送请求响应接口
 */
export interface CanSendRequestResponse {
  canSend: boolean
}

// ============================================================================
// 好友分组相关API类型
// ============================================================================

/**
 * 创建好友分组请求接口
 */
export interface CreateFriendGroupRequest {
  userId: number
  name: string
  description?: string
}

/**
 * 更新好友分组请求接口
 */
export interface UpdateFriendGroupRequest {
  userId: number
  name: string
  description?: string
}

/**
 * 调整分组排序请求接口
 */
export interface ReorderGroupsRequest {
  userId: number
  groupIds: number[]
}

/**
 * 移动好友到分组请求接口
 */
export interface MoveFriendRequest {
  userId: number
  friendId: number
  groupId?: number
}

/**
 * 批量移动好友请求接口
 */
export interface MoveFriendsRequest {
  userId: number
  friendIds: number[]
  groupId?: number
}

/**
 * 分组好友数量响应接口
 */
export interface FriendCountResponse {
  count: number
}

// ============================================================================
// 隐私设置相关API类型
// ============================================================================

/**
 * 更新隐私设置请求接口
 */
export interface UpdatePrivacySettingRequest {
  friendRequestPermission?: 'EVERYONE' | 'FRIENDS_OF_FRIENDS' | 'CONTACTS_ONLY' | 'NOBODY'
  requireVerification?: boolean
  allowSearchByPhone?: boolean
  allowSearchByEmail?: boolean
  allowSearchByUsername?: boolean
  showOnlineStatus?: boolean
  allowGroupInvite?: boolean
}

/**
 * 是否可以被搜索响应接口
 */
export interface CanBeSearchedResponse {
  canBeSearched: boolean
}

/**
 * 是否可以接收好友请求响应接口
 */
export interface CanReceiveFriendRequestResponse {
  canReceive: boolean
}

// ============================================================================
// WebSocket/RSocket 消息类型
// ============================================================================

/**
 * 实时消息类型枚举
 */
export type RealTimeMessageType = 
  | 'NEW_MESSAGE'
  | 'MESSAGE_READ'
  | 'MESSAGE_DELIVERED'
  | 'USER_ONLINE'
  | 'USER_OFFLINE'
  | 'TYPING_START'
  | 'TYPING_END'
  | 'CONVERSATION_UPDATED'
  | 'SYSTEM_NOTIFICATION'
  | 'FRIEND_REQUEST_RECEIVED'
  | 'FRIEND_REQUEST_ACCEPTED'
  | 'FRIEND_REQUEST_REJECTED'
  | 'FRIEND_REQUEST_CANCELLED'
  | 'NEW_FRIEND_ADDED'
  | 'FRIEND_REMOVED'

/**
 * 实时消息接口
 */
export interface RealTimeMessage {
  type: RealTimeMessageType
  data: any
  timestamp: Date
  fromUserId?: number
  conversationId?: string
}

/**
 * 新消息推送数据
 */
export interface NewMessagePush {
  message: Message
  conversation: Conversation
}

/**
 * 消息状态更新推送数据
 */
export interface MessageStatusPush {
  messageId: string
  status: MessageStatus
  readAt?: Date
  deliveredAt?: Date
}

/**
 * 用户状态更新推送数据
 */
export interface UserStatusPush {
  userId: number
  status: UserStatus
  lastActiveTime: Date
}

/**
 * 打字状态推送数据
 */
export interface TypingStatusPush {
  userId: number
  conversationId: string
  isTyping: boolean
}

/**
 * 好友请求通知推送数据
 */
export interface FriendRequestNotificationPush {
  type: 'FRIEND_REQUEST_RECEIVED' | 'FRIEND_REQUEST_ACCEPTED' | 'FRIEND_REQUEST_REJECTED' | 'FRIEND_REQUEST_CANCELLED'
  message: string
  fromUserId: number
  toUserId: number
  timestamp: Date
  data: {
    requestId?: number
    message?: string
    rejectReason?: string
    processedAt?: Date
    expiredAt?: Date
  }
}

/**
 * 新好友添加通知推送数据
 */
export interface NewFriendNotificationPush {
  type: 'NEW_FRIEND_ADDED'
  message: string
  fromUserId: number
  toUserId: number
  timestamp: Date
  data: {
    newFriendId: number
    addedAt: Date
  }
}

/**
 * 好友删除通知推送数据
 */
export interface FriendRemovedNotificationPush {
  type: 'FRIEND_REMOVED'
  message: string
  fromUserId: number
  toUserId: number
  timestamp: Date
  data: {
    removedFriendId: number
    removedAt: Date
  }
}

// ============================================================================
// 应用状态类型
// ============================================================================

/**
 * 认证状态接口
 */
export interface AuthState {
  user: User | null
  token: string | null
  refreshToken: string | null
  isAuthenticated: boolean
  isLoading: boolean
  error: string | null
}

/**
 * 消息状态接口
 */
export interface MessagesState {
  messagesByConversation: Record<string, Message[]>
  sendingMessages: Record<string, Message>
  isLoading: boolean
  error: string | null
  hasMoreMessages: Record<string, boolean>
}

/**
 * 好友状态接口
 */
export interface FriendsState {
  friends: Friend[]
  onlineFriends: number[]
  isLoading: boolean
  error: string | null
  searchResults: Friend[]
  isSearching: boolean
}

/**
 * 连接状态接口
 */
export interface ConnectionState {
  isConnected: boolean
  isConnecting: boolean
  reconnectAttempts: number
  lastConnectedAt: Date | null
  error: string | null
}

/**
 * 会话状态接口
 */
export interface ConversationsState {
  conversations: Conversation[]
  currentConversationId: string | null
  isLoading: boolean
  error: string | null
  totalUnreadCount: number
}

/**
 * 应用根状态接口
 */
export interface AppState {
  auth: AuthState
  messages: MessagesState
  friends: FriendsState
  connection: ConnectionState
  conversations: ConversationsState
}

// ============================================================================
// 工具类型和验证类型
// ============================================================================

/**
 * 创建消息的输入类型
 */
export type CreateMessageInput = Omit<Message, 'messageId' | 'createdAt' | 'sequence' | 'status' | 'senderNickname' | 'senderAvatarUrl'>

/**
 * 更新消息的输入类型
 */
export type UpdateMessageInput = Partial<Pick<Message, 'status' | 'isRead'>>

/**
 * 创建会话的输入类型
 */
export type CreateConversationInput = Omit<Conversation, 'id' | 'createdAt' | 'updatedAt' | 'lastMessage' | 'unreadCount'>

/**
 * 文件上传响应接口
 */
export interface FileUploadResponse {
  fileId: string
  fileName: string
  fileSize: number
  fileUrl: string
  thumbnailUrl?: string
  mimeType: string
  uploadedAt: Date
}

/**
 * 错误响应接口
 */
export interface ErrorResponse {
  error: string
  message: string
  code: number
  details?: Record<string, any>
  timestamp: Date
}